<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
// load base class if needed
require_once( APPPATH . 'controllers/base/adminbase.php' );
class poli extends adminbase {

    //constructor
    public function poli() {
        parent::__construct();
        //load librray notifikasi
        $this->load->library('tnotification');
        //load library pagination
        $this->load->library('pagination');
        //load model
        $this->load->model('poli/m_poli');
    }

    public function index() {
        $this->page();
    }
    function page() {
        //display pagination
        $strTotalRow = $this->m_poli->count();
        $strConfig['base_url'] = site_url("poli/poli/page");
        $strConfig['total_rows'] = $strTotalRow;
        $strConfig['per_page'] = 20;
        $strConfig['uri_segment'] = 4;
        $strConfig['cur_tag_open'] = "<a class='current'>";
        $strConfig['cur_tag_close'] = '</a>';
        $this->pagination->initialize($strConfig);
        $strPagination = $this->pagination->create_links();
        $strPage = $this->uri->segment(4);

        $strLimit = $strConfig['per_page'];
        if (!$strPage) {
            $strPage = 0;
        }
        // bisnis proses
        $arrParams = array(intval($strPage), $strLimit);
        $strData = $this->m_poli->getAllPoliWithLimit($arrParams);
   
        //url
        $this->smarty->assign("base_url", site_url());
        $this->smarty->assign("strUrllist", site_url("poli/poli"));
        $this->smarty->assign("strUrladd", site_url("poli/poli/add"));
        $this->smarty->assign("strUrldel", site_url("poli/poli/processDelete"));
        $this->smarty->assign("strUrledit", site_url("poli/poli/edit"));
        $this->smarty->assign("strUrldetail", site_url("poli/poli/detail"));
        
         //url sidebar
        $this->smarty->assign("strUrlAdmin", site_url("admin/admin"));
        $this->smarty->assign("strUrlPegawai", site_url("pegawai/pegawai"));
        $this->smarty->assign("strUrlPasien", site_url("pasien/pasien"));
        $this->smarty->assign("strUrlJenisObat", site_url("jenisobat/jenisobat"));
        $this->smarty->assign("strUrlObat", site_url("obat/obat"));
        $this->smarty->assign("strUrlPoliklinik", site_url("poli/poli"));
        
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();

        //set breadcrumb
        $strBreadcrumbHtml = "<a href='" . base_url() . "index.php/home/welcomeadmin'>Home </a>&raquo; <b>poli</b>";
        $this->smarty->assign("strBreadcrumbUrl", $strBreadcrumbHtml);

        //assign variable
        $this->smarty->assign("strPoli", $strData);
        
        $this->smarty->assign("strPagination", $strPagination);
        // set template content
        $this->smarty->assign("strTemplateContent", "poli/list.html");
        // output
        parent::display();
    }

    public function add() {
        // set template content
        $this->smarty->assign("strTemplateContent", "poli/add.html");
        //bussiness process
        $strHtmlPoliList = "";
        $strPoliList = $this->m_poli->getAllPoli();
        foreach ($strPoliList as $strPoli) {
            $strHtmlPoliList .= "<option value='" . $strPoli['id_poli'] . "'>" . $strPoli['nama'] . "</option>";
        }
        //url tab
        $this->smarty->assign("base_url", site_url());
        $this->smarty->assign("strUrllist", site_url("poli/poli"));
        $this->smarty->assign("strUrladd", site_url("poli/poli/add"));
        $this->smarty->assign("strUrlsave", site_url("poli/poli/processAdd"));
         //url sidebar
        $this->smarty->assign("strUrlAdmin", site_url("admin/admin"));
        $this->smarty->assign("strUrlpoli", site_url("poli/poli"));
        $this->smarty->assign("strUrlPegawai", site_url("pegawai/pegawai"));
        $this->smarty->assign("strUrlPasien", site_url("pasien/pasien"));
        $this->smarty->assign("strUrlJenisObat", site_url("jenisobat/jenisobat"));
        $this->smarty->assign("strUrlObat", site_url("obat/obat"));
        $this->smarty->assign("strUrlPoliklinik", site_url("poliklinik/poliklinik"));
        
        //set breadcrumb
        $strBreadcrumbHtml = "<a href='" . base_url() . "index.php/home/welcomeadmin'>Home </a>&raquo; <a href='" . base_url() .
                "index.php/poli/poli'>poli </a>&raquo; <b> Tambah</b>";
        $this->smarty->assign("strBreadcrumbUrl", $strBreadcrumbHtml);

        //assign variable
        $this->smarty->assign("strHtmlPoliList", $strHtmlPoliList);
        
        //display notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    public function processAdd() {
        $this->tnotification->set_rules('nama', 'Nama Poli', 'trim|required|max_length[20]');
        if ($this->tnotification->run() != FALSE) {
            $strSessData = $this->session->userdata('session_admin');
            $arrParam = array($this->input->post('nama'));
            if (($this->m_poli->saveAdd($arrParam))) {// == TRUE
                //display success notif
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Data berhasil disimpan");
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal disimpan");
            }
        } else {
            $this->tnotification->sent_notification("error", "Data gagal disimpan");
        }
        redirect('poli/poli/add');
    }
    public function edit($pStrId) {
        // set template content
        $this->smarty->assign("strTemplateContent", "poli/edit.html");

        $strPoli = $this->m_poli->getPoli($pStrId);
        //url tab
        $this->smarty->assign("base_url", site_url());
        $this->smarty->assign("strUrllist", site_url("poli/poli"));
        $this->smarty->assign("strUrladd", site_url("poli/poli/add"));
        $this->smarty->assign("strUrlsave", site_url("poli/poli/processEdit"));
       
         //url sidebar
        $this->smarty->assign("strUrlAdmin", site_url("admin/admin"));
        $this->smarty->assign("strUrlpoli", site_url("poli/poli"));
        $this->smarty->assign("strUrlPegawai", site_url("pegawai/pegawai"));
        $this->smarty->assign("strUrlPasien", site_url("pasien/pasien"));
        $this->smarty->assign("strUrlJenisObat", site_url("jenisobat/jenisobat"));
        $this->smarty->assign("strUrlObat", site_url("obat/obat"));
        $this->smarty->assign("strUrlPoliklinik", site_url("poliklinik/poliklinik"));
        
        //assign role data
        $this->smarty->assign("strPoliData", $strPoli);
       
        //display notif
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        //set breadcrumb
        $strBreadcrumbHtml = "<a href='" . base_url() . "index.php/home/welcomeadmin'>Home </a>&raquo; <a href='" . base_url() .
                "index.php/poli/poli'>poli </a>&raquo; <b> Edit poli</b>";
        $this->smarty->assign("strBreadcrumbUrl", $strBreadcrumbHtml);
        // output
        parent::display();
    }
    public function processEdit() {
      $this->tnotification->set_rules('name', 'Nama poli', 'trim|required|max_length[20]');
      $this->tnotification->set_rules('username', 'Username', 'trim|required|max_length[20]');
      $this->tnotification->set_rules('password', 'Password', 'trim|required|max_length[20]');
//        $this->tnotification->set_rules('jenis_kelamin', 'Jenis Kelamin', 'trim|required');
//        $this->tnotification->set_rules('kota_lahir', 'Kota Lahir', 'trim|required');
//        $this->tnotification->set_rules('tgl_lahir', 'Tgl Lahir', 'trim|required');
//        $this->tnotification->set_rules('telp', 'Telp', 'trim|required');
//        $this->tnotification->set_rules('alamat', 'Alamat', 'trim|required');
//        $this->tnotification->set_rules('tgl_masuk', 'Tgl Masuk', 'trim|required');
     $this->tnotification->set_rules('poli', 'Poli', 'trim|required');

        if ($this->tnotification->run() != FALSE) {
            $strSessData = $this->session->userdata('session_admin');
             $arrParam = array($this->input->post('name'),
                $this->input->post('username'),
                $this->input->post('password'),
                $this->input->post('jenis_kelamin'),
                $this->input->post('kota_lahir'),
                $this->input->post('tgl_lahir'),
                $this->input->post('telp'),
                $this->input->post('alamat'),
                $this->input->post('tgl_masuk'),
                $this->input->post('foto'),
                $this->input->post('poli'),
                $this->input->post('id_poli'));
            if ($this->m_poli->saveEdit($arrParam)) {
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Data berhasil diubah");
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal diubah");
            }
        } else {
            $this->tnotification->sent_notification("error", "Data gagal diubah");
        }
        redirect('poli/poli/edit/' . $this->input->post('id_poli'));
    }

    public function processDelete() {
        //check rule
        $this->tnotification->set_rules('id_poli[]', 'Kode', 'trim|required');
        $strData = $this->input->post('id_poli');
        if ($this->tnotification->run() != FALSE) {
            foreach ($strData as $strVal) {
                // bisnis proses 
                if ($this->m_poli->delete($strVal)) {
                    $this->tnotification->sent_notification("success", "Data berhasil dihapus");
                } else {
                    $this->tnotification->sent_notification("error", "Data gagal dihapus");
                }
            }
        } else {
            $this->tnotification->sent_notification("error", "Silakan pilih data yang akan dihapus terlebih dahulu");
        }
        redirect('poli/poli/');
    }

}

?>